<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
use yii\grid\GridView;
use kartik\export\ExportMenu;

/* @var $this yii\web\View */
/* @var $searchModel app\models\WritingsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '著作';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="writings-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]);  ?>

    <div class="col-md-12" style="margin-bottom: 15px;">      
        <?php
        $form = ActiveForm::begin([
                    'action' => ['index'],
                    'method' => 'get',
                    'options' => ['class' => 'input-group'],
                    'fieldConfig' => [
                        'template' => '<div class="col-lg-12">{input}</div>',
                    ]
                ])
        ?>

        <div class="col-md-4">
            <label style="float: left;margin-top: 5px;">项目名称</label>
            <div style="overflow: hidden;">
                <?php
//                $data = app\models\Writings::find()->all();
//                echo $form->field($searchModel, 'writingname')->widget(Select2::classname(), [
//                    'data' => ArrayHelper::map($data, 'writingname', 'writingname'),
//                    'options' => ['placeholder' => '请输入内容'],
//                    'pluginOptions' => [
//                        'allowClear' => true
//                    ],
//                ]);
                ?>  
                 <?php
                $datas = app\models\Writings::find()->all();
                if ($datas != null) {
                    foreach ($datas as $item) {
                        $source[] = $item->writingname;
                    }
                } else {
                    $source[] = array();
                }
                echo yii\jui\AutoComplete::widget([
                    'model' => $searchModel,
                    'attribute' => 'writingname',
                    'options'=>['style'=>'margin-left:10px'],
                    'clientOptions' => [
                        'source' => $source,
                    ],
                ]);
                ?>
            </div>
        </div>
        <div class="col-md-4">
            <label style="float: left;margin-top: 5px;">著作编号</label>
            <div style="overflow: hidden;">
                 <?php
                if ($datas != null) {
                    foreach ($datas as $item) {
                        $source[] = $item->writingno;
                    }
                } else {
                    $source[] = array();
                }
                echo yii\jui\AutoComplete::widget([
                    'model' => $searchModel,
                    'attribute' => 'writingno',
                    'options'=>['style'=>'margin-left:10px'],
                    'clientOptions' => [
                        'source' => $source,
                    ],
                ]);
                ?>
            </div>
        </div>

        <div class="col-md-3" style="padding:0px;margin:0px">
            <label style="float: left;margin-top: 5px;">作者</label>
            <div style="overflow: hidden;">
                <?php
//                $data = app\models\Writings::find()->all();
//                echo $form->field($searchModel, 'author')->widget(Select2::classname(), [
//                    'data' => ArrayHelper::map($data, 'author', 'author'),
//                    'options' => ['placeholder' => '请输入内容'],
//                    'pluginOptions' => [
//                        'allowClear' => true
//                    ],
//                ]);
                ?>
                 <?php
                if ($datas != null) {
                    foreach ($datas as $item) {
                        $source[] = $item->author;
                    }
                } else {
                    $source[] = array();
                }
                echo yii\jui\AutoComplete::widget([
                    'model' => $searchModel,
                    'attribute' => 'author',
                    'options'=>['style'=>'margin-left:10px'],
                    'clientOptions' => [
                        'source' => $source,
                    ],
                ]);
                ?>
            </div>
        </div>

        <div class="col-md-1 text-center">
            <?= Html::submitButton(' 搜索', ['class' => 'btn btn-primary glyphicon glyphicon-search']) ?> 
        </div>

    </div>
    
    <div class="col-md-5 text-right" style="margin: 10px 0 15px 15px;float: right;padding-right: 0">

        <?= Html::a('新增', ['create'], ['class' => 'btn btn-primary']) ?> 
        <button class="btn btn-default glyphicon glyphicon-print" onclick="printme()"> 打印</button>
        <span>&nbsp;&nbsp;</span>
        
                <!--为什么要多加一个form才可以导出呢？？？-->
        <?php
        $form = ActiveForm::begin([
                    'action' => ['index'],
                    'options' => ['enctype' => 'multipart/form-data', 'id' => 'form2', 'name' => 'form2']
                ])
        ?>
        <?php ActiveForm::end(); ?>
        
        <?Php
        echo ExportMenu::widget([
            'dataProvider' => $dataProvider,
            'dropdownOptions' => [
                'label' => '导出Excel',
                'class' => 'btn btn-default',
                'icon' => '',
            ],
            'showColumnSelector' => false,
            'exportConfig' => [
                ExportMenu::FORMAT_TEXT => false,
                ExportMenu::FORMAT_PDF => false,
                ExportMenu::FORMAT_CSV => false,
                ExportMenu::FORMAT_EXCEL_X => false,
                ExportMenu::FORMAT_HTML => false
            ],
            'columns' => [
                //  ['class' => 'yii\grid\SerialColumn'],
                // 'id',
                'writingname',
                'writingno',
                'publishdate',
                'author',
                'writinglevel',
                'mark',
                'updatetime',
                'isfinish',
                'addman',
                'addmanid',
                'addtime',
                'writingtype',
            ]
        ]);
        ?>  
    </div>

    <?php ActiveForm::end(); ?> 

    <!--加载上传文件的对话框-->
    <?= $this->render('/upload/upload', ['model' => $model]) ?>

    <?php Pjax::begin(); ?>
    <?=
    GridView::widget([
        'id' => 'tableContainer',
        'dataProvider' => $dataProvider,
        'columns' => [
            // ['class' => 'yii\grid\SerialColumn'],
            //'id',

            [
                'attribute' => 'writingname',
                'label' => '著作名称',
                'format' => 'raw',
                'value' => function ($model) {
                    return Html::a($model->writingname, ['writings/view', 'id' => $model->id]);
                },
                        'contentOptions' => ['style' => ''],
                        'headerOptions' => ['style' => ''],
                    ],
                    'writingno',
                    'publishdate',
                    'author',
                    'writinglevel',
                    // 'mark',
                    // 'updatetime',
                    // 'isfinish',
                    // 'addman',
                    // 'addmanid',
                    // 'addtime',
                    // 'writingtype',
                    [
                        'class' => 'yii\grid\ActionColumn',
                        'template' => '{update} {delete}',
                        'buttons' => [
                            //update button
                            'update' => function ($url) {
                                return Html::a('<span class="fa fa-search"></span>编辑', $url, [
                                            'title' => Yii::t('app', 'Update'),
                                                // 'class' => 'btn btn-primary btn-md',
                                ]);
                            },
                                    //update button
                                    'delete' => function ($url) {
                                return Html::a('<span class="fa fa-search"></span>删除', $url, [
                                            'title' => Yii::t('app', 'Delete'),
                                            'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                                            'data-method' => 'post',
                                            'data-pjax' => '0',
                                                //  'class' => 'btn btn-primary btn-md',
                                ]);
                            },
                                ],
                            ],
                        ],
                    ]);
                    ?>
                    <?php Pjax::end(); ?>
</div>
<script>
    var global_Html = "";
    function printme() {
        global_Html = document.body.innerHTML;
        document.body.innerHTML = $('#tableContainer').html();
        $('.btn').hide();
        $('.pagination').hide();
        $('a').each(function () {
            $(this).replaceWith($(this).html());
        });
        window.print();
        window.setTimeout(function () {
            document.body.innerHTML = global_Html;
        }, 300);
    }
</script>
